package com.demo.apparel.controller.web;

import com.demo.apparel.common.Result;
import com.demo.apparel.dto.web.AdminInfoUpdateDTO;
import com.demo.apparel.dto.web.AdminQueryDTO;
import com.demo.apparel.entity.AdminInfo;
import com.demo.apparel.service.web.AdminInfoService;
import com.demo.apparel.utils.JwtUtils;
import com.demo.apparel.vo.PageVO;
import com.demo.apparel.vo.web.AdminVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/web/adminInfo")
@Tag(name = "管理员资料", description = "管理员基本信息接口")
public class AdminInfoController {

    @Autowired
    private JwtUtils jwtUtils;

    @Autowired
    private AdminInfoService adminInfoService;

    @GetMapping("/info")
    @Operation(summary = "获取管理员基本信息")
    public Result<AdminInfo> getInfo(HttpServletRequest request) {
        String adminId = (String) request.getAttribute("userId");
        return Result.success(adminInfoService.getInfo(adminId));
    }

    @PutMapping("/update")
    @Operation(summary = "更新管理员基本信息")
    public Result<Void> updateInfo(@RequestBody @Valid AdminInfoUpdateDTO dto, HttpServletRequest request) {
        String adminId = (String) request.getAttribute("userId");
        adminInfoService.updateInfo(adminId, dto);
        return Result.success();
    }

    @PostMapping("/list")
    @Operation(summary = "获取所有管理员信息")
    public Result<PageVO<AdminVO>> list(@RequestBody AdminQueryDTO dto) {
        PageVO<AdminVO> page = adminInfoService.getAdminList(dto);
        return Result.success(page);
    }

    // 头像上传目录（建议后期改成配置）
    private static final String ADMIN_AVATAR_PATH = System.getProperty("user.dir") + "/admin/avatar/";
    @Operation(summary = "上传管理员头像")
    @PostMapping(value = "/upload/avatar", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result<Map<String, String>> uploadAvatar(
            @Parameter(description = "上传的头像文件", required = true)
            @RequestPart("file") MultipartFile file
    ) throws IOException {
        if (file == null || file.isEmpty()) {
            return (Result<Map<String, String>>) Result.error("文件不能为空","ww");
        }
        // 获取文件扩展名
        String originalFilename = file.getOriginalFilename();
        String ext = "";
        if (originalFilename != null && originalFilename.contains(".")) {
            ext = originalFilename.substring(originalFilename.lastIndexOf("."));
        }
        // 生成唯一文件名
        String fileName = UUID.randomUUID().toString().replace("-", "") + ext;
        // 创建目标目录
        File dir = new File(ADMIN_AVATAR_PATH);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        // 保存文件
        File dest = new File(dir, fileName);
        file.transferTo(dest);
        // 构建文件访问路径
        String fileUrl = "/static/admin/avatar/" + fileName;
        // 返回结构
        Map<String, String> data = new HashMap<>();
        data.put("fileUrl", fileUrl);
        return Result.success(data);
    }
}
